package com.ophelisis.actions.menuMgmt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ophelisis.persistence.dao.CategoryDAO;
import com.ophelisis.persistence.dao.MenuGroupDAO;
import com.ophelisis.persistence.domain.CategoryVO;
import com.ophelisis.persistence.domain.MenuGroupVO;

/** <pre>
 * Class : com.ophelisis.actions.menuMgmt.CategoryMgmt / CategoryMgmt.java
 * 	Define Class Object : 
 * <br>
 * </pre>
 * @author hlee / apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2013. 07. 29.
 */
@Controller
public class CategoryMgmt {
	@Autowired
	CategoryDAO categoryDAO;
	
	@Autowired
	MenuGroupDAO menuGroupDAO;
	
	private List<CategoryVO> categoryList;
	private List<MenuGroupVO> menuGroupList;
	private String sResult = "";
	
	private Log log = LogFactory.getLog(this.getClass());
	private static final String _LogPrefixStr = "CodeMgmt Log> ";
	
	@RequestMapping(value="categoryMgmt.action")
	public ModelAndView categoryMgmt(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value="target", defaultValue="cmMain") String sTarget,
			@RequestParam(value="_groupId", defaultValue="0") String sMenuGroupId,
			@RequestParam(value="_menuId", defaultValue="0") String sMenuId,
			@RequestParam(value="_menuNm", defaultValue="") String sMenuNm,
			@RequestParam(value="_menuAuth", defaultValue="0") String sUserMenuAuth,
			@RequestParam(value="_prevFirstCode", defaultValue="") String sPrevFirstCode,
			@RequestParam(value="_prevSecondCode", defaultValue="") String sPrevSecondCode,
			@RequestParam(value="_prevThirdCode", defaultValue="") String sPrevThirdCode,
			@RequestParam(value="isNew", defaultValue="Y") String isNew,
			@ModelAttribute CategoryVO categoryVO) throws Exception {
		ModelAndView mav = new ModelAndView();

		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		log.debug(_LogPrefixStr + "|" + " CategoryMgmt called !!!");
		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");

		try {
			// 공통 요소
			mav.addObject("_groupId", sMenuGroupId);
			mav.addObject("_menuId", sMenuId);
			mav.addObject("_menuNm", sMenuNm);
			mav.addObject("_menuAuth", sUserMenuAuth);
			mav.addObject("_prevFirstCode", sPrevFirstCode);
			mav.addObject("_prevSecondCode", sPrevSecondCode);
			mav.addObject("_prevThirdCode", sPrevThirdCode);
			
			Map<String, Object> params = new HashMap<String, Object>();
			
			if (sTarget.equals("cmMain")) {
				mav = returnMgmt("main", mav, params);
			} else if (sTarget.equals("cmList")) {
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("cmSave")) {
				try {
					sResult = (isNew.equals("Y") ? categoryDAO.setCategoryAdd(categoryVO) : categoryDAO.setCategoryModify(categoryVO)) > 0 ? "정상적으로 처리되었습니다." : "처리에 실패하였습니다.<BR/>관리자에게 문의하여 주십시오."; // 정보 업데이트

					if (!isNew.equals("Y") && categoryVO.getCategoryType().equals("1")) {
						params.put("categoryFirst", categoryVO.getCategoryId());
						params.put("menuGroupId", categoryVO.getMenuGroupId());
						
						categoryDAO.setCategoryModifyMenuGroupId(params);
						params.clear();
					}
				} catch (Exception e) {
					log.error("DB 정보 획득 failed!, categoryId=" + categoryVO.getCategoryId(), e);
				}
				mav.addObject("result", sResult);
				
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("cmDelete")) {
				try {
					sResult = categoryDAO.setCategoryDelete(categoryVO) > 0 ? "정상적으로 처리되었습니다." : "처리에 실패하였습니다.<BR/>관리자에게 문의하여 주십시오."; // 정보 업데이트
				} catch (Exception e) {
					log.error("DB 정보 획득 failed!, categoryId=" + categoryVO.getCategoryId(), e);
				}
				mav.addObject("result", sResult);
				
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("mmExist")) {
				params.clear();
				params.put("categoryId", categoryVO.getCategoryId());
				params.put("categoryType", categoryVO.getCategoryType());

				int resultCount = categoryDAO.existCategory(params);
	
				mav.addObject("result", resultCount);
				
				mav.setViewName("/WEB-INF/jsp/common/ajaxResult.jsp");
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return mav;
	}
	
	public ModelAndView returnMgmt(
			@RequestParam(value="cmd", defaultValue="list") String cmd,
			@ModelAttribute ModelAndView mav,
			Map<String, Object> params) throws Exception {
		if (cmd.equals("main")) {
			mav.setViewName("/WEB-INF/jsp/menuMgmt/categoryMgmt.jsp");

			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " CategoryMgmt Main-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		} else if (cmd.equals("list")) {
			try {
				categoryList = categoryDAO.getCategoryList(params);
				
				menuGroupList = menuGroupDAO.getMenuGroupList(params);
			} catch (Exception e) {
				log.error("DB 정보 획득 failed!", e);
			}

			mav.addObject("categoryList", categoryList);
			mav.addObject("menuGroupList", menuGroupList);
			mav.setViewName("/WEB-INF/jsp/menuMgmt/categoryMgmtList.jsp");
			
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " CategoryMgmt List-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		}
		
		return mav;
	}
}

